﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Index.cshtml";
}
<script src="@SiteUrl.GetStaticResourceScriptUrl("~/js/wdtree/tree.js")"></script>
<link href="@SiteUrl.GetStaticResourceScriptUrl("~/js/wdtree/tree.css")" rel="stylesheet" />
<style>
    .dv-left {
        width: 200px;
        float: left;
    }

    .dv-left-tree {
        overflow-y: auto;
        border-radius: 4px;
        background: white;
    }

    .dv-left-title {
        padding: 6px 10px;
        border-bottom: 1px solid transparent;
        border-top-left-radius: 3px;
        border-top-right-radius: 3px;
        border-color: #ddd;
        background-color: white;
    }

    .dv-right {
        float: left;
        margin-left: 5px;
    }

    .dv-right-order {
        width: 100%;
    }

    .dv-grid {
        width: 100%;
    }
</style>
<div class="dv-body">
    <div class="dv-left">
        <div class="dv-left-title">
            患者列表
        </div>
        <div class="dv-left-tree" id="PatTreeList"></div>
    </div>
    <div class="dv-right">
        <table class="form" style="background-color:white">
            <tr>
                <td class="formTitle">姓名：</td>
                <td class="formValue"><label id="hzxm"></label></td>
                <td class="formTitle">性别：</td>
                <td class="formValue" style="width:170px"><label id="sex"></label></td>
                <td class="formTitle">年龄：</td>
                <td class="formValue"><label id="nl"></label></td>
            </tr>
            <tr>
                <td class="formTitle"> 执行日期：</td>
                <td class="formValue">
                    <input id="txt_zxrq" type="text" class="form-control input-wdatepicker" style="width:70%;"
                           onclick="WdatePicker({ dateFmt: 'yyyy-MM-dd' })"
                           onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd', onpicked: function () { RefreshOrder() } })" value="@DateTime.Now.ToString("yyyy-MM-dd ")" />
                </td>
                <td class="formTitle">
                    执行类别:
                </td>
                <td class="formValue">
                    <select class="form-control" id="zxdlb">
                        <option value="">请选择</option>
                        <option value="zsz">注射证</option>
                        <option value="whd">雾化单</option>
                        <option value="jyd">检验单</option>
                        <option value="jcd">检查单</option>
                    </select>
                </td>
                <td class="formValue">
                    <div class="btn-group">
                        <a id="btn_search" class="btn btn-primary">
                            查询
                        </a>
                    </div>
                </td>
                <td class="formValue">
                    <div class="btn-group">
                        <a id="ExecutionChecked" class="btn btn-primary" onclick="ExecutionChecked()">
                            <i class="fa fa-pencil-square-o"></i>打印
                        </a>
                    </div>
                </td>
            </tr>
        </table>
        <br />
        <div class="gridPanel">
            <table id="gridList"></table>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(".dv-body").height($(window).height());
    $(".dv-body").width($(window).width());
    var rightWidth = $(".Newtouch_iframe", parent.document).width() - $(".dv-left").width() - 35;
    $(".dv-right").width(rightWidth);
    $(".dv-left-tree").height($(window).height() - 70);
    var zyh = "";

    $(function () {
        $(window).resize(function () {//浏览器窗口调整大小时重新计算宽高
            $(".dv-body").height($(window).height());
            $(".dv-body").width($(window).width());
        });
        $('#gridList').jqGrid("clearGridData");

        getPatTreeList();
        gridList("");
    });

    function getPatTreeList() {
        $("#PatTreeList").treeview({
            height: $(window).height() - 35,
            slimscroll: false,
            showcheck: false,
            url: "@Url.Action("GetPatWardTree")",
            onnodeclick: function (item) {
                if (item.value && item.Ex1 == "c") {
                    zyh = item.value;
                    $('#hzxm').html(item.Ex4);
                    $('#sex').html($.getGender(item.Ex2));
                    $('#nl').html(item.Ex3+"岁");
                    $('#gridList').jqGrid('setGridParam', {
                        postData: { zyh: zyh, zxsj: $('#txt_zxrq').val(), zxdlb: $('#zxdlb').val()  }
                    }).trigger("reloadGrid");
                }
            }
        });
        $("#btn_search").click(function () {
            var patcheck = $("#PatTreeList").getCurrentNode();
            if (patcheck.Ex1 == "c") {
                $("#gridList").jqGrid('setGridParam', {
                    postData: { zyh: zyh, zxsj: $('#txt_zxrq').val(), zxdlb: $('#zxdlb').val()  },
                }).trigger('reloadGrid');
            }

        });
    }

    function getOrderExecutionList(item, s) {
        var patlist = $("#BqTreeList").getCheckedNodeObjArray();
        var patlistFath = "";

        var patzyh = "";

        //当前选中父节点
        var len = 0;
        var single = 0; //是否单个子节点

        if (item != null && item.Ex1 == "p") {
            patlistFath = item.ChildNodes;
            len = patlistFath.length;
        }
        else if (item != null && item.Ex1 == "c") {
            patlistFath = item;
            len = 1;
            single = 1;
        }

        //选中节点
        for (var i = 0; i < patlist.length; i++) {
            if (patlist[i].Ex1 == "c") {
                patzyh += patlist[i].value + ",";
            }
        }

        if (s == 1) {
            if (single == 0) {
                for (var j = 0; j < len; j++) {
                    if (patlistFath[j].Ex1 == "c") {
                        patzyh += patlistFath[j].value + ",";
                    }
                }
            }
            else if (patlistFath.Ex1 == "c") {
                patzyh += patlistFath.value;
            }
        }
        else if (s == 0) {
            if (single == 0) {
                for (var j = 0; j < len; j++) {
                    if (patlistFath[j].Ex1 == "c" && patlistFath[j].checkstate == 1) {
                        patzyh = patzyh.replace(patlistFath[j].value + ",", "");
                    }
                }
            }
            else if (patlistFath.Ex1 == "c") {
                patzyh = patzyh.replace(patlistFath.value + ",", "");
            }

        }

        //$('#gridList').jqGrid("clearGridData");
        $('#gridList').jqGrid('setGridParam', {
            postData: {
                patList: patzyh, zxsj: $('#txt_zxrq').val(), zxdlb: $('#zxdlb').val()
            }
        }).trigger("reloadGrid");
    }


    function getOrderListByPatId(patid) {
        $('#gridList').jqGrid("clearGridData");
        $('#gridList').newtouchLocalDataGrid(null, orderList);
    }

    //级联选中所有子节点
    function checkAllSon(node) {
        $('#BqTreeList').treeview('checkNode', node.nodeId, { silent: true });
        if (node.nodes != null && node.nodes.length > 0) {
            for (var i in node.nodes) {
                checkAllSon(node.nodes[i]);
            }
        }
    }
    //级联取消所有子节点
    function uncheckAllSon(node) {
        $('#BqTreeList').treeview('uncheckNode', node.nodeId, { silent: true });
        if (node.nodes != null && node.nodes.length > 0) {
            for (var i in node.nodes) {
                uncheckAllSon(node.nodes[i]);
            }
        }
    }
    var checkTrigger = false;
    var list = new Array();
    var liststu = false;
    var j = 0;
    function gridList(patzyh) {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            multiselect: true,
            postData: { zyh: zyh, zxsj: $('#txt_zxrq').val(), zxdlb: $('#zxdlb').val() },
            url: "@Url.Action("GetExecDetailGridJson")",
            height: $(window).height() - 125,
            colModel: [
                { label: '医嘱ID', name: 'yzId', align: 'center', width: 50, key: true, hidden: true },
                { label: '', name: 'clbz', align: 'center', width: 50, hidden: true },
                { label: '医嘱号', name: 'yzh', align: 'center', width: 50, hidden: true },
                { label: '分组号', name: 'zh', align: 'center', width: 100 },
                { label: '医嘱', name: 'yznr', align: 'center', width: 300 },
                { label: '执行时间', name: 'zxsj', align: 'center', width: 120, formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' } },
                { label: '审核者', name: 'shz', align: 'center', width: 70 },
                { label: '执行者', name: 'zxz', align: 'center', width: 70 },
                { label: 'yzlx', name: 'yzlx', align: 'center', hidden: true },
                { label: 'ypyfdm', name: 'ypyfdm', align: 'center', hidden: true }
            ],
            pager: "#gridPager",
            sortname: 'zyh asc,kssj asc',
            viewrecords: true,
            multiselect: true,
            onSelectRow: function (rowIndex, s) {
                if (!checkTrigger) {
                    var len = 0;
                    var rowData = $("#gridList").jqGrid('getRowData', rowIndex);
                    var ids = $("#gridList").jqGrid('getRowData_AllLine');

                    liststu = s; //同步状态
                    for (var i = 0; i < ids.length; i++) {
                        if (ids[i].zh == rowData.zh && ids[i].zh != "") {
                            if (s == true) {
                                if (rowIndex != ids[i].yzId) {
                                    list[len] = ids[i].yzId;
                                    len += 1;
                                }
                            }
                            else {
                                if (rowIndex != ids[i].yzId) {
                                    list[len] = ids[i].yzId;
                                    len += 1;
                                }
                            }
                        }
                    }
                    checkTrigger = true;
                }

                if (list.length > 0) {

                    for (j = j; j < list.length; j++) {
                        if (liststu) //同组选中
                        {
                            j = j + 1;
                            $("#gridList").jqGrid("setSelection", list[j - 1], true);

                        }
                        else { //同组取消选中
                            //j = j + 1;
                            $("#gridList").jqGrid("setSelection", list[j], false);      //取消不触发onselectrow
                        }

                    }
                }

                j = 0;
                checkTrigger = false;
                list = new Array();
                liststu = false;
            },
        });
    }
    //刷新医嘱
    function RefreshOrder() {
        $('#gridList').jqGrid("clearGridData");
        getOrderExecutionList(null, null);
    }

    //执行当前
    function ExecutionChecked() {
        //选中的id
        var selRowIds = jQuery("#gridList").jqGrid("getGridParam", "selarrrow");
        if (selRowIds == null || selRowIds.length == 0) {
            $.modalAlert("请先选中需执行的医嘱", 'warning');
            return;
        }
        var reportServerHOST = '@(ViewBag.ReportServerHOST)';
        var orgId = '@(ViewBag.OrgId)';
        for (var i = 0; i < selRowIds.length; i++) {
            var rowData = $("#gridList").jqGrid('getRowData', selRowIds[i]);
            if (rowData.yzlx == '@((int)EnumYzlx.jy)') {
                window.open(reportServerHOST + "/ReportServer/Pages/ReportViewer.aspx?%2fNewtouch.Report.1%2f%E8%B4%B5%E5%AE%89%E4%BD%8F%E9%99%A2%E6%A3%80%E9%AA%8C%E7%94%B3%E8%AF%B7%E5%8D%95&yzdh=" + rowData.yzh + "&zyh=" + zyh + "&orgId=" + orgId,"_blank", "height=500, width=1195,top=100, left=50, toolbar =no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no");
            } else if (rowData.yzlx == '@((int)EnumYzlx.jc)') {
                window.open(reportServerHOST + "/ReportServer/Pages/ReportViewer.aspx?%2fNewtouch.Report.1%2f%E4%BD%8F%E9%99%A2%E6%A3%80%E6%9F%A5%E7%94%B3%E8%AF%B7%E5%8D%95&yzdh=" + rowData.yzh + "&zyh=" + zyh + "&orgId=" + orgId, "_blank", "height=500, width=1195,top=100, left=50, toolbar =no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no");
            } else if ('@ViewBag.ControlzsyfCode' != "" && '@ViewBag.ControlzsyfCode'.indexOfIgnoreCase(rowData.ypyfdm) > -1) {//注射证
                window.open(reportServerHOST + "/ReportServer/Pages/ReportViewer.aspx?%2fNewtouch.Report.1%2f%E8%B4%B5%E5%AE%89%E4%BD%8F%E9%99%A2%E6%B3%A8%E5%B0%84%E5%8D%95&yzdh=" + rowData.yzh + "&zyh=" + zyh + "&orgId=" + orgId, "_blank", "height=500, width=1195,top=100, left=50, toolbar =no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no");
            } else if ('@ViewBag.ControlwhyfCode' != "" && '@ViewBag.ControlwhyfCode'.indexOfIgnoreCase(rowData.ypyfdm) > -1) {//雾化单
                window.open(reportServerHOST + "/ReportServer/Pages/ReportViewer.aspx?%2fNewtouch.Report.1%2f%E8%B4%B5%E5%AE%89%E4%BD%8F%E9%99%A2%E9%9B%BE%E5%8C%96%E5%8D%95&yzdh=" + rowData.yzh + "&zyh=" + zyh + "&orgId=" + orgId, "_blank", "height=500, width=1195,top=100, left=50, toolbar =no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no");
            }
        }
    }
</script>